// pages/teacher/teacherStu/teacherStu.js
var app = getApp();
import {post} from "../../../../asserts/js/request";
import {teacher_active} from "../../../../asserts/js/common/barList"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: app.globalData.bottom + 'px',
    stuList: [],
    barList: teacher_active(0),
    classId: '',
    studentName: '',
    allChoose: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      classId: options.id
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  back: function() {
    wx.navigateBack({
      delta: 0,
    })
  },
  next: function() {
    var this_ = this;

    var flag = false;
    outer:
    for(var item of this.data.stuList){
      for(var el of item.studentList){
        if(el.active){
          flag = true;
          break outer;
        }
      }
    }
    if(!flag) {
      wx.showToast({
        title: '请选择学员',
        icon: 'none'
      })
      return;
    }

    wx.setStorageSync('subList', this.data.stuList);
    wx.navigateTo({
      url: '/pages/teacher/myclass/teacherCourse/teacherCourse?classId=' + this_.data.classId,
    })
  },
  getAll: function() {
    let this_ = this;
    var list = this.data.stuList.map(item => {
      item.subList = item.studentList.map(el => {
        el.active = !this_.data.allChoose;
        return el;
      })
      return item;
    });
    this.setData({
      stuList: list,
      allChoose: !this_.data.allChoose
    })
  },
  stuChoose: function(e) {

    console.log(e.detail.value);
    this.setData({
      ["stuList[" + e.detail.itemindex + "].studentList["+ e.detail.elindex +"].active"]: e.detail.value
    })
    if(e.detail.value) {
      var flag = true;
      toFor:
      for(var item of this.data.stuList){
        for(var el of item.studentList) {
          if(!el.active){
            flag = false;
            break toFor;
          }
        }
      }
      this.setData({
        allChoose: flag
      })
    }else {
      this.setData({
        allChoose: false
      })
    }
  },
  getData: function(){
    let this_ = this
    post("queryStudent",{
      classId: this.data.classId,
      studentName: this.data.studentName
    }).then(data => {
      console.log(data);
      this_.setData({
        stuList: data.studentInfo
      })
      wx.setStorageSync('subList', data.studentInfo.map(item => {
        item.active = false;
        item.studentList = item.studentList.map(el => {
          el.active = false;
          el.courseShow = false;
          el.courseList = [];
          return el;
        })
        return item;
      }));
    }).catch(err => {
      console.log(err);
    });
  },
  nameChange: function(e) {
    this.setData({
      studentName: e.detail.value
    })
  }
})